package com.controller;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.beans.CartInfo;
import com.service.CartService;

@Controller  @RequestMapping("/cart")
public class CartController {
	@Resource
	private CartService cartService;
	
	//全局的,用于转向某个页面
	@GetMapping("/{page}")
	public String toPage(@PathVariable("page") String page) {
		return page;   //默认会转向 src/main/resources/templates/+page+.html
	}
	
	@PostMapping("/add")
	public String add(CartInfo cart,ModelMap m)
	{
		cartService.addCart(cart);
		m.put("msg","添加成功");
		return "cart-add";
	}
	
	@GetMapping("/checkAll")
	public String checkAll(ModelMap m) {
		List<CartInfo> cartList=cartService.select_all();
		m.put("cartList",cartList);
		m.put("size",cartList.size());
		return "cart-checkAll";
	}
	
	@GetMapping("/getAll")
	public String getAll(ModelMap m) {
		List<CartInfo> cartList=cartService.select_all();
		m.put("cartList",cartList);
		m.put("size",cartList.size());
		return "cart-manager";
	}
	
	//根据id查询信息，转到修改页
		@GetMapping("/update")
		public String searchForUpdate(int id,ModelMap m) {
			CartInfo cart=cartService.getCart(id);
			m.put("cart",cart);
			return "cart-update";
		}
		
		//进行修改
		@PostMapping("/update")
		public String update(CartInfo cart,ModelMap m) {
			m.put("cart",cart);
			cartService.updateCart(cart);
			m.put("msg","信息更新成功");
			return "cart-update";
		}
		
		@GetMapping("/delete")
		public String delete(int id) {
			cartService.delCart(id);
			return "forward:/cart/getAll";
		}

}
